package com.flag.liutao.modules.user.controller;


import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.IdsParam;
import com.flag.liutao.common.domain.QueryParam;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.common.enums.UserStatusEnum;
import com.flag.liutao.common.utils.JwtUtil;
import com.flag.liutao.modules.user.param.UserCountQueryParam;
import com.flag.liutao.modules.user.param.UserParam;
import com.flag.liutao.modules.user.param.UserQueryParam;
import com.flag.liutao.modules.user.service.IUserService;
import com.flag.liutao.modules.user.vo.UserQueryVo;
import com.flag.liutao.modules.user.vo.UserVo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-11-18
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@Api(tags = "用户管理")
public class UserController {

    private final IUserService userService;

    /**
     * 获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/admin/userinfo")
    @PreAuthorize("hasAuthority('sys_power:login_admin')")
    @ApiOperation(value="获取用户信息",notes = "用户管理：获取用户信息")
    public ResponseResult<UserVo> getUserInfo(HttpServletRequest request){
        String authorization = request.getHeader("Authorization");
        String token = authorization.split(" ")[1];
        String uid;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            uid = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token认证失败");
        }
        UserVo user = userService.getUserInfoById(uid);
        return ResponseResult.ok(user);
    }

    /**
     * 分页获取用户信息
     * @param param
     * @return
     */
    @GetMapping("/admin/user/page")
    @PreAuthorize("hasAuthority('sys_power:user_R')")
    @ApiOperation(value="分页获取用户信息",notes = "用户管理：分页获取用户信息")
    public ResponseResult<Map<String,Object>> getUserPage(UserQueryParam param){
        Map<String,Object> map = userService.getUserPage(param.getPage(),param.getLimit(),param.getKeyword(),param.getGradeId(),param.getMajorId(),param.getDeptId(),param.getUnitId(),param.getRoleId(),param.getStatus());
        return ResponseResult.ok(map);
    }



    /**
     * 获取用户详情信息
     * @param id
     * @return
     */
    @GetMapping("/admin/user/{id}")
    @PreAuthorize("hasAuthority('sys_power:user_R')")
    @ApiOperation(value="分页获取用户信息",notes = "用户管理：分页获取用户信息")
    public ResponseResult<UserQueryVo> getUserInfo(@PathVariable Long id){
        UserQueryVo result  = userService.getUserQueryInfoById(id);
        return ResponseResult.ok(result);
    }



    /**
     * 添加用户信息
     * @param param
     * @return
     */
    @PostMapping("/admin/user/add")
    @PreAuthorize("hasAuthority('sys_power:user_C')")
    @ApiOperation(value = "添加用户信息",notes = "用户管理：添加用户信息")
    public ResponseResult<Boolean> addUser(@Validated @RequestBody UserParam param){
        userService.addUser(param);
        return ResponseResult.ok();
    }

    /**
     * 修改用户信息
     * @param param
     * @return
     */
    @PostMapping("/admin/user/edit")
    @PreAuthorize("hasAuthority('sys_power:user_U')")
    @ApiOperation(value = "修改用户信息",notes = "用户管理：修改用户信息")
    public ResponseResult<Boolean> editUser(@Validated @RequestBody UserParam param){
        userService.editUser(param);
        return ResponseResult.ok();
    }
    
    /**
     * 删除用户信息
     * @param param
     * @return
     */
    @PostMapping("/admin/user/del")
    @PreAuthorize("hasAuthority('sys_power:user_D')")
    @ApiOperation(value = "删除用户信息",notes = "用户管理：删除用户信息")
    public ResponseResult<Boolean> delUser(@Validated @RequestBody IdParam param){
        userService.delUser(param.getId());
        return ResponseResult.ok();
    }


    /**
     * 批量删除用户信息
     * @param param
     * @return
     */
    @PostMapping("/admin/user/dels")
    @PreAuthorize("hasAuthority('sys_power:user_D')")
    @ApiOperation(value = "批量删除用户信息",notes = "用户管理：批量删除用户信息")
    public ResponseResult<Boolean> delsUser(@Validated @RequestBody IdsParam param){
        for (Long id : param.getIds()) {
        userService.delUser(id);
        }
        return ResponseResult.ok();
    }

    /**
     * 修改队员状态
     * @param param
     * @return
     */
    @PostMapping("/admin/user/editStatus")
    @PreAuthorize("hasAuthority('sys_power:user_status')")
    @ApiOperation(value = "修改用户状态",notes = "用户管理：修改用户状态")
    public ResponseResult<Boolean> editUserStatus(@Validated @RequestBody UserParam param){
        userService.editUseStatus(param.getStatus(),param.getId());
        return ResponseResult.ok();
    }


    /**
     * 转正
     * @param param
     * @return
     */
    @PostMapping("/admin/user/regular")
    @PreAuthorize("hasAuthority('sys_power:user_regular')")
    @ApiOperation(value = "转正",notes = "用户管理：转正")
    public ResponseResult<Boolean> becomeRegularUser(@Validated @RequestBody IdsParam param){
        for (Long id : param.getIds()) {
            userService.editUseStatus(UserStatusEnum.STATUS_1.getValue(),id);
        }
        return ResponseResult.ok();
    }

    /**
     * 退队
     * @param param
     * @return
     */
    @PostMapping("/admin/user/leave")
    @PreAuthorize("hasAuthority('sys_power:user_leave')")
    @ApiOperation(value = "退队",notes = "用户管理：退队")
    public ResponseResult<Boolean> becomeLeaveUser(@Validated @RequestBody IdsParam param){
        for (Long id : param.getIds()) {
            userService.editUseStatus(UserStatusEnum.STATUS_LEAVE.getValue(),id);
        }
        return ResponseResult.ok();
    }

    /**
     * 退役
     * @param param
     * @return
     */
    @PostMapping("/admin/user/retire")
    @PreAuthorize("hasAuthority('sys_power:user_retire')")
    @ApiOperation(value = "退役",notes = "用户管理：退役")
    public ResponseResult<Boolean> becomeRetireUser(@Validated @RequestBody IdsParam param){
        for (Long id : param.getIds()) {
            userService.editUseStatus(UserStatusEnum.STATUS_RETIRE.getValue(),id);
        }
        return ResponseResult.ok();
    }


    /**
     * 获取每个分队队员数目
     */
    @GetMapping("/admin/user/count")
    @ApiOperation(value = "获取分队队员数目",notes = "用户管理：获取分队队员数目")
    public ResponseResult<Map<String,Object>> getUserCountByUnit(UserCountQueryParam param){
        Map<String,Object> map =userService.getUserCountByUnit(param);
        return ResponseResult.ok(map);
    }

}
